package com.jiangjiang.eshop.commodity.controller;

import com.google.common.collect.Lists;
import com.jiangjiang.eshop.commodity.service.GoodsDetailPictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName GoodsDetailPictureController
 * @Description 商品详情图片管理
 * @Author jhw
 * @Date 2021/8/2 16:13
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/goods/detail/picture")
public class GoodsDetailPictureController {

    @Autowired
    private GoodsDetailPictureService goodsDetailPictureService;

    /**
     * 批量上传图片
     * @param goodsDetailId 商品明细id
     * @param pictures 图片数据
     * @return 图片路由
     */
    @PostMapping("/batchUpload")
    public List<String> batchUpload(Long goodsDetailId, MultipartFile[] pictures){
        try {
            return goodsDetailPictureService.batchUpload(goodsDetailId,pictures);
        }catch (Exception e){
            log.warn("error",e);
        }
        return Lists.newArrayList();
    }

    /**
     * 根据商品id删除图片
     * @param goodsDetailId 商品明细id
     */
    @DeleteMapping("/{goodsDetailId}")
    public Boolean batchRemoveByGoodsId(
            @PathVariable("goodsDetailId") Long goodsDetailId) throws Exception {
        try {
            return goodsDetailPictureService.batchRemoveByGoodsDetailId(goodsDetailId);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }
}
